package org.hcf.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.hcf.data.entity.ChatGroupInfo;
import org.hcf.data.mapper.ChatGroupInfoMapper;
import org.hcf.data.service.IChatGroupInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 群组成员关系表 服务实现类
 * </p>
 *
 * @author xianiyitiao
 * @since 2024-04-18
 */
@Service
public class ChatGroupInfoServiceImpl extends ServiceImpl<ChatGroupInfoMapper, ChatGroupInfo> implements IChatGroupInfoService {

    @Override
    public List<ChatGroupInfo> getGroupInfo(long userId) {
        return list(new LambdaQueryWrapper<ChatGroupInfo>().eq(ChatGroupInfo::getUserId, userId));
    }

    @Override
    public List<ChatGroupInfo> getGroupInfoByGroup(long groupId) {
        return list(new LambdaQueryWrapper<ChatGroupInfo>().eq(ChatGroupInfo::getGroupId, groupId));
    }

    @Override
    public List<ChatGroupInfo> getGroupInfoByGroups(List<Long> groupIds) {
        return list(new LambdaQueryWrapper<ChatGroupInfo>().in(ChatGroupInfo::getGroupId, groupIds));
    }

    @Override
    public List<ChatGroupInfo> getGroupInfos(Long[] ids) {
        return list(new LambdaQueryWrapper<ChatGroupInfo>().in(ChatGroupInfo::getInfoId, ids));
    }

    @Override
    public List<Long> getFriendsByLastTime(long userId, long lastMsgRemark) {
        return list(new LambdaQueryWrapper<ChatGroupInfo>().select(ChatGroupInfo::getGroupId).eq(ChatGroupInfo::getUserId, userId).gt(ChatGroupInfo::getModifyTime, lastMsgRemark)).stream().map(ChatGroupInfo::getGroupId).collect(Collectors.toList());
    }

    @Override
    public ChatGroupInfo getGroupInfo(long userId, long groupId) {
        return getOne(new LambdaQueryWrapper<ChatGroupInfo>().eq(ChatGroupInfo::getGroupId, groupId).eq(ChatGroupInfo::getUserId, userId).last("limit 1"));
    }
}
